
//引入接口访问类
import { ClassicModel} from '../../models/classic.js'
import { LikeModel } from '../../models/like.js'

const classicModel = new ClassicModel()
const likeModel = new LikeModel()

Page({

  /**
   * 页面的初始数据
   */
  //因为每次进入页面直接获取了最新一期期刊，所以可以凭此设置first和last
  data: {
    classicData: null,
    newest:true,
    oldest:false,
    likeStatus:false,
    likeNums:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    classicModel.getLastest((res)=>{
      this.setData({
        classicData:res,
        likeStatus: res.like_status,
        likeNums: res.fav_nums
      })
    })
  },
  //点赞
  clickLike:function(event){
    let behavior = event.detail.behavior
    likeModel.updateLike(behavior, this.data.classicData.id, this.data.classicData.type)

  },
  //onPrevious和onNext中有很多公共部分，我们做一下重构,添加方法onchangeClassic,提取公共部分,通过传递参数nextorPervious来告知调用的接口
  //上一期
  onPrevious:function(){
    this.onchangeClassic('previous')
  },
  //下一期
  onNext:function(){
    this.onchangeClassic('next')
  },
  onchangeClassic(nextorPervious){
    let index = this.data.classicData.index
    classicModel.getClassic(index, nextorPervious, (res) => {
      this.setData({
        classicData: res,
        newest: classicModel.isLasted(res.index),
        oldest: classicModel.isFirsted(res.index)
      })
      this._getLikeStatus(res.id, res.type)
    })
  },
  //单独获取点赞信息
  _getLikeStatus(art_id,art_type){
    likeModel.getClassicLikeStatus(art_id, art_type,(res)=>{
      this.setData({
        likeStatus: res.like_status,
        likeNums: res.fav_nums
      })
    })
  }
})